﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class ProductAdditionalPropertyRepository : IProductAdditionalPropertyRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IProductAdditionalPropertyRepository Members

		public int CreateNewProductAdditionalProperty(Data.Entity.ProductAdditionalProperty theNewProductAdditionalProperty)
		{
			if (theNewProductAdditionalProperty == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_ProductAddtnlProperty_Create(theNewProductAdditionalProperty.ProductID, theNewProductAdditionalProperty.PropertyName,
					theNewProductAdditionalProperty.PropertyValue, theNewProductAdditionalProperty.DisplayOrder);
			}

			return result;
		}

		public IList<Data.Entity.ProductAdditionalProperty> GetProductAdditionalProperties(int productID)
		{
			if (productID <= 0)
			{
				return null;
			}

			List<ECommerce_GetProductAdditionalPropertiesResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetProductAdditionalProperties(productID).OrderBy(exp => exp.DisplayOrder);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.ProductAdditionalProperty>();

				foreach(var ap in lstData)
				{
					var theEntity = new Data.Entity.ProductAdditionalProperty()
					{
						ProductAdditionalPropertyID = ap.ProductAdditionalPropertyID,
						DisplayOrder = ap.DisplayOrder,
						ProductID = ap.ProductID,
						PropertyName = ap.PropertyName,
						PropertyValue = ap.PropertyValue
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int UpdateProductAdditionalProperty(Data.Entity.ProductAdditionalProperty theProductAdditionalProperty)
		{
			if (theProductAdditionalProperty == null || theProductAdditionalProperty.ProductAdditionalPropertyID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProductAdditionalProperty = (from pap in _dbContext.ProductAdditionalProperties
												   where pap.ProductAdditionalPropertyID == theProductAdditionalProperty.ProductAdditionalPropertyID
												   select pap).FirstOrDefault();

				if(dbProductAdditionalProperty == null)
				{
					result = 0;
				}
				else
				{
					dbProductAdditionalProperty.DisplayOrder = theProductAdditionalProperty.DisplayOrder;
					dbProductAdditionalProperty.ProductID = theProductAdditionalProperty.ProductID;
					dbProductAdditionalProperty.PropertyName = theProductAdditionalProperty.PropertyName;
					dbProductAdditionalProperty.PropertyValue = theProductAdditionalProperty.PropertyValue;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		public bool DeleteProductAdditionalProperty(int id)
		{
			if (id <= 0)
			{
				return false;
			}

			bool result = false;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbProductAdditionalProperty = (from ap in _dbContext.ProductAdditionalProperties
												   where ap.ProductAdditionalPropertyID.Equals(id)
												   select ap).FirstOrDefault();

				if(dbProductAdditionalProperty == null)
				{
					result = false;
				}
				else
				{
					_dbContext.ProductAdditionalProperties.DeleteOnSubmit(dbProductAdditionalProperty);
					_dbContext.SubmitChanges();

					result = true;
				}
			}

			return result;
		}

		public void SaveTheCustomerMustKnownContent(Data.Entity.ProductAdditionalProperty theCustomerMustKnownContent)
		{
			if (theCustomerMustKnownContent == null)
			{
				return;
			}

			using (_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbCustomerMustKnownContent = (from pa in _dbContext.ProductAdditionalProperties
												  where pa.PropertyName.Equals(theCustomerMustKnownContent.PropertyName) && pa.ProductID == null
												  select pa).FirstOrDefault();

				bool isNew = false;

				if (dbCustomerMustKnownContent == null)
				{
					dbCustomerMustKnownContent = new ProductAdditionalProperty();
					dbCustomerMustKnownContent.PropertyName = theCustomerMustKnownContent.PropertyName;

					isNew = true;
				}

				dbCustomerMustKnownContent.PropertyValue = theCustomerMustKnownContent.PropertyValue;

				if (isNew)
				{
					_dbContext.ProductAdditionalProperties.InsertOnSubmit(dbCustomerMustKnownContent);
				}

				_dbContext.SubmitChanges();
			}
		}

		public Data.Entity.ProductAdditionalProperty GetTheProductAdditionalPropertyByName(string propertyName)
		{
			if (string.IsNullOrEmpty(propertyName))
			{
				return null;
			}

			List<Data.ProductAdditionalProperty> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ProductAdditionalProperties.Where(exp => exp.ProductID == null && exp.PropertyName.Equals(propertyName));

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var pa = lstData[0];

				Data.Entity.ProductAdditionalProperty result;

				result = new Data.Entity.ProductAdditionalProperty()
				{
					DisplayOrder = pa.DisplayOrder,
					ProductAdditionalPropertyID = pa.ProductAdditionalPropertyID,
					ProductID = pa.ProductID,
					PropertyValue = pa.PropertyValue,
					PropertyName = pa.PropertyName
				};

				return result;
			}

			return null;
		}

		#endregion
	}
}
